package com.loong.android;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;

import java.lang.ref.WeakReference;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class LonApp extends Application {
    private static LonApp instance;
    //维护Activity的List
    private static final List<Activity> mActivities = Collections.synchronizedList(new LinkedList<Activity>());
    //为避免内存泄漏使用弱引用
    private WeakReference<Activity> mCurrentActivity;

    public static void finishAllActivity() {
        if (mActivities.isEmpty()) return;
        for (Activity activity : mActivities) {
            activity.finish();
        }
    }

    /**
     * 获取Application实例
     */
    public static LonApp getInstance() {
        return instance;
    }

    /**
     * 获取应用上下文
     */
    public static Context getAppContext() {
        if(instance == null) return null;
        return instance.getApplicationContext();
    }

    /**
     * 获取应用包名
     */
    public static String getAppPackageName() {
        if(instance == null) return null;
        return instance.getApplicationContext().getPackageName();
    }

    /**
     * 取应用版本号
     */
    public static int getAppVersionCode() {
        if(instance == null) return 0;
        Context context = instance.getApplicationContext();
        int versioncode = 0;
        try {
            PackageManager pm = context.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
            versioncode = pi.versionCode;
        } catch (Exception ignore) {

        }
        return versioncode;
    }

    /**
     * 取SDK版本
     */
    public static int getSDKVersion() {
        return Build.VERSION.SDK_INT;
    }

    /**
     * 取应用版本名
     * @return
     */
    public static String getAppVersionName() {
        if(instance == null) return null;
        Context context = instance.getApplicationContext();
        String versioncode = null;
        try {
            PackageManager pm = context.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
            versioncode = pi.versionName;
        } catch (Exception ignore) {
        }
        return versioncode;
    }

    /**
     * 获取到当前最上层的activity
     */
    public Activity getCurrentActivity() {
        if(mCurrentActivity == null) return null;
        return mCurrentActivity.get();
    }

    /**
     * 获取到当前最上层的窗口对象
     */
    public LonWin getCurrentWin() {
        if(mCurrentActivity == null) return null;
        Activity win = mCurrentActivity.get();
        return win instanceof LonWin ? (LonWin) win : null;
    }

    /**
     * 取文件授权
     */
    public static String getFileAuthority() {
        return instance.getApplicationContext().getPackageName() + "." + LonFileProvider.class.getSimpleName();
    }

    @Override
    public void onCreate () {
        super.onCreate();
        instance = this;
        registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated (Activity activity, Bundle bundle) {
                mActivities.add(activity);
                //在此获取一次Activity, 防止在activity的onCreate方法内获取此值时为null
                mCurrentActivity = new WeakReference<>(activity);
                onWinCreate(activity, bundle);
            }

            @Override
            public void onActivityStarted (Activity activity) {
                onWinStart(activity);
            }

            @Override
            public void onActivityResumed (Activity activity) {
                mCurrentActivity = new WeakReference<>(activity);
                onWinReady(activity);
            }

            @Override
            public void onActivityPaused (Activity activity) {
                onWinPause(activity);
            }

            @Override
            public void onActivityStopped (Activity activity) {
                onWinStop(activity);
            }

            @Override
            public void onActivitySaveInstanceState (Activity activity, Bundle bundle) {
                onWinSaveInstanceState(activity, bundle);
            }

            @Override
            public void onActivityDestroyed (Activity activity) {
                if (!mActivities.isEmpty()) {
                    mActivities.remove(activity);
                    onWinDestroy(activity);
                }
            }
        });
        onAppCreate();
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        onAppTerminate();
    }

    /**
     * 应用被创建
     */
    protected void onAppCreate() {

    }

    /**
     * 应用被终止
     */
    protected void onAppTerminate() {

    }

    /**
     * 窗口被创建
     * @param win 窗口对象
     * @param bundle 绑定数据对象
     */
    protected void onWinCreate(Activity win, Bundle bundle) {

    }

    /**
     * 窗口保存实例状态完毕
     * @param win 窗口对象
     * @param bundle 绑定数据对象
     */
    protected void onWinSaveInstanceState(Activity win, Bundle bundle) {

    }

    /**
     * 窗口就绪完毕
     * @param win 窗口对象
     */
    protected void onWinReady(Activity win) {

    }

    /**
     * 窗口启动完毕
     * @param win 窗口对象
     */
    protected void onWinStart(Activity win) {

    }

    /**
     * 窗口被暂停
     * @param win 窗口对象
     */
    protected void onWinPause(Activity win) {

    }

    /**
     * 窗口被停止
     * @param win 窗口对象
     */
    protected void onWinStop(Activity win) {

    }

    /**
     * 窗口被销毁
     * @param win 窗口对象
     */
    protected void onWinDestroy(Activity win) {

    }
}
